<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>物料列表</title>
    <link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../../css/style.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        .submenu { display: none; }
        .menu-item.active > .submenu { display: block; }
        .menu-arrow { transition: transform 0.2s; }
        .menu-item.active > a > .menu-arrow { transform: rotate(90deg); }
        .table th { background-color: #f8f9fa; }
        .submenu li a.active { color: #28a745; font-weight: bold; transform: translateY(2px); display: inline-block; }
        .search-area { background: #f8f9fa; padding: 10px; border-radius: 8px; margin: 10px 0; width: 100%; }
        .search-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
        .search-item { flex: 0 0 auto; margin-right: 10px; margin-bottom: 5px; }
        .search-item .form-control, .search-item .form-select { width: 120px; height: 31px; font-size: 14px; }
        .search-item .btn { padding: 4px 8px; }
        .search-label { display: block; margin-bottom: 5px; font-size: 14px; color: #555; white-space: nowrap; }
        .table-responsive { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
        .custom-table { width: 100%; border-collapse: separate; border-spacing: 0; }
        .custom-table th { background: #f8f9fa; color: #495057; font-weight: 600; padding: 12px 15px; border-bottom: 2px solid #dee2e6; white-space: nowrap; }
        .custom-table td { padding: 12px 15px; border-bottom: 1px solid #eee; vertical-align: middle; }
        .action-buttons { display: flex; gap: 8px; }
        .action-buttons .btn {
            padding: 4px 10px;
            font-size: 13px;
            min-width: 60px;
            white-space: nowrap;
        }
        .page-title { font-size: 22px; font-weight: 600; margin-bottom: 20px; color: #333; display: flex; align-items: center; gap: 10px; }
        .page-title-icon { font-size: 24px; }
        .ms-auto { margin-left: auto; }
        .add-button { padding: 8px 16px; display: flex; align-items: center; gap: 5px; font-weight: 500; }
        .content-wrapper { padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin: 20px; }
        /* iOS风格模态框样式 */
        .ios-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1050;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .ios-modal.show {
            display: flex;
            opacity: 1;
        }

        .ios-modal-content {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 0;
            max-width: 400px;
            width: 90%;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            transform: scale(0.8);
            transition: transform 0.3s ease;
        }

        .ios-modal.show .ios-modal-content {
            transform: scale(1);
        }

        .ios-modal-header {
            padding: 20px 20px 10px;
            text-align: center;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }

        .ios-modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .ios-modal-body {
            padding: 20px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .ios-detail-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        }

        .ios-detail-item:last-child {
            border-bottom: none;
        }

        .ios-detail-label {
            font-weight: 500;
            color: #666;
            font-size: 14px;
        }

        .ios-detail-value {
            font-weight: 400;
            color: #333;
            font-size: 14px;
            text-align: right;
            max-width: 60%;
            word-wrap: break-word;
        }

        .ios-modal-footer {
            padding: 15px 20px 20px;
            text-align: center;
        }

        .ios-close-btn {
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 12px 30px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.2s ease;
        }

        .ios-close-btn:hover {
            background: #0056CC;
        }

        /* 滚动条样式 */
        .ios-modal-body::-webkit-scrollbar {
            width: 4px;
        }

        .ios-modal-body::-webkit-scrollbar-track {
            background: transparent;
        }

        .ios-modal-body::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.2);
            border-radius: 2px;
        }
    </style>
</head>
<body>
<div class="admin-layout">
    <!-- 顶部导航栏 -->
    <header class="admin-header">
        <div class="header-left">
            <div class="logo">生产过程管理系统</div>
        </div>
        <div class="header-right">
            <span class="user-info">部门经理</span>
            <span class="user-name">
                    <script>
                        try {
                            const userInfo = localStorage.getItem('userInfo');
                            const user = JSON.parse(userInfo);
                            document.write(user.name || '未登录');
                        } catch (e) {
                            document.write('未登录');
                        }
                    </script>
                </span>
            <button class="logout-btn" onclick="logout()">退出</button>
        </div>
    </header>
    <!-- 左侧菜单 -->
    <aside class="admin-sidebar">
        <ul class="menu-list">
            <li class="menu-item">
                <a href="homepage/manager_home.html">
                    <span class="menu-icon">🏠</span>
                    <span class="menu-title">首页</span>
                </a>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">👥</span>
                    <span class="menu-title">人员管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../person/person_list.html">人员列表</a></li>
                    <li><a href="../person/person_edit.html">添加人员</a></li>
                    <li><a href="../person/schedule_edit.html">编辑排班表</a></li>
                    <li><a href="../person/schedule.html">查看排班表</a></li>
                    <li><a href="../person/schedule_statistics.html">班次统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📦</span>
                    <span class="menu-title">物料管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../material/material_list.html">物料列表</a></li>
                    <li><a href="*">物料采购统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">⚙️</span>
                    <span class="menu-title">设备管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../device/device_list.html" class="active">设备列表</a></li>
                    <li><a href="*">设备报修报废统计</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📝</span>
                    <span class="menu-title">产品管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../product/product_list.html">产品列表</a></li>
                    <li><a href="*">编辑生产计划</a></li>
                    <li><a href="*">查看生产计划</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">🏢</span>
                    <span class="menu-title">供应商管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../provider/provider_list.html">供应商列表</a></li>
                </ul>
            </li>
        </ul>
    </aside>
    <!-- 主内容区域 -->
    <main class="admin-main">
        <div class="content-wrapper">
            <div class="page-title">
                <span class="page-title-icon">⚙️</span>
                <span>设备列表</span>

            </div>
            <!-- 搜索区域 -->
            <div class="search-area">
                <div class="search-row">
                    <div class="search-item">
                        <label class="search-label">设备编号</label>
                        <input type="text" class="form-control form-control-sm" id="searchNumber" placeholder="请输入编号">
                    </div>
                    <div class="search-item">
                        <label class="search-label">设备名称</label>
                        <input type="text" class="form-control form-control-sm" id="searchName" placeholder="请输入名称">
                    </div>
                    <div class="search-item">
                        <label class="search-label">设备类型</label>
                        <select class="form-select form-select-sm" id="searchType">
                            <option value="">全部</option>
                            <option value="重型">重型</option>
                            <option value="中型">中型</option>
                            <option value="轻型">轻型</option>
                            <option value="便携式">便携式</option>
                            <option value="其它">其它</option>
                        </select>
                    </div>
                    <div class="search-item">
                        <label class="search-label">制造商</label>
                        <input type="text" class="form-control form-control-sm" id="searchManufacturer" placeholder="请输入制造商">
                    </div>
                    <div class="search-item">
                        <button class="btn btn-sm btn-outline-primary" onclick="searchDevices()">🔍</button>
                    </div>
                    <div class="search-item">
                        <button class="btn btn-sm btn-outline-secondary" onclick="resetSearch()">↺</button>
                    </div>
                </div>
            </div>
            <!-- 数据表格 -->
            <div class="table-responsive">
                <table class="table table-hover table-striped custom-table mb-0">
                    <thead>
                    <tr>
                        <th>设备编号</th>
                        <th>设备名称</th>
                        <th>设备类型</th>
                        <th>制造商</th>
                        <th>生产日期</th>
                        <th>保修期限</th>
                        <th>售后联系人</th>
                        <th>售后电话</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="deviceTableBody">
                    <!-- 数据将通过JavaScript动态加载 -->
                    </tbody>
                </table>
            </div>
        </div>
    </main>
</div>
<!-- iOS风格设备详情模态框 -->
<div class="ios-modal" id="deviceDetailModal">
    <div class="ios-modal-content">
        <div class="ios-modal-header">
            <h5 class="ios-modal-title">设备详情</h5>
        </div>
        <div class="ios-modal-body">
            <div class="ios-detail-item">
                <span class="ios-detail-label">设备编号</span>
                <span class="ios-detail-value" id="detailDeviceNumber">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">设备名称</span>
                <span class="ios-detail-value" id="detailDeviceName">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">设备类型</span>
                <span class="ios-detail-value" id="detailDeviceType">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">制造商</span>
                <span class="ios-detail-value" id="detailManufacturer">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">生产日期</span>
                <span class="ios-detail-value" id="detailManufactureDate">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">保修期限</span>
                <span class="ios-detail-value" id="detailWarrantyPeriod">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">售后联系人</span>
                <span class="ios-detail-value" id="detailAfterSalesContact">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">售后电话</span>
                <span class="ios-detail-value" id="detailAfterSalesPhone">-</span>
            </div>
            <div class="ios-detail-item">
                <span class="ios-detail-label">保修说明</span>
                <span class="ios-detail-value" id="detailWarrantyDesc">-</span>
            </div>
        </div>
        <div class="ios-modal-footer">
            <button class="ios-close-btn" onclick="closeDeviceDetailModal()">关闭</button>
        </div>
    </div>
</div>
<script src="../../assets/js/bootstrap.bundle.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });

        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function (e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = '../homepage/manager_home.html'; // 跳转到管理员首页
        });

        // 默认展开设备管理菜单
        const deviceMenuItem = document.querySelector('.menu-item:nth-child(4)');
        if (deviceMenuItem) {
            deviceMenuItem.classList.add('active');
        }
        // 设置当前页面对应的菜单项为选中状态
        const currentPath = window.location.pathname;
        const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
        document.querySelectorAll('.submenu li a').forEach(link => {
            if (link.getAttribute('href') === filename) {
                link.classList.add('active');
            }
        });
        // 加载设备数据
        loadDevices();
    });

    async function loadDevices() {
        try {
            const response = await fetch('http://localhost:8080/api/devices');
            const data = await response.json();
            displayDevices(data);
        } catch (error) {
            console.error('加载设备数据失败:', error);
        }
    }

    function searchDevices() {
        const searchParams = {
            deviceNumber: document.getElementById('searchNumber').value.toLowerCase(),
            deviceName: document.getElementById('searchName').value.toLowerCase(),
            deviceType: document.getElementById('searchType').value,
            manufacturer: document.getElementById('searchManufacturer').value.toLowerCase()
        };
        const tbody = document.getElementById('deviceTableBody');
        const rows = tbody.getElementsByTagName('tr');
        for (let row of rows) {
            const cells = row.getElementsByTagName('td');
            const rowData = {
                deviceNumber: cells[0].textContent.toLowerCase(),
                deviceName: cells[1].textContent.toLowerCase(),
                deviceType: cells[2].textContent,
                manufacturer: cells[3].textContent.toLowerCase()
            };
            let visible = true;
            for (const [key, value] of Object.entries(searchParams)) {
                if (value && !rowData[key].includes(value)) {
                    visible = false;
                    break;
                }
            }
            row.style.display = visible ? '' : 'none';
        }
    }

    function resetSearch() {
        document.getElementById('searchNumber').value = '';
        document.getElementById('searchName').value = '';
        document.getElementById('searchType').value = '';
        document.getElementById('searchManufacturer').value = '';
        const tbody = document.getElementById('deviceTableBody');
        const rows = tbody.getElementsByTagName('tr');
        for (let row of rows) {
            row.style.display = '';
        }
    }

    function displayDevices(devices) {
        const tbody = document.getElementById('deviceTableBody');
        tbody.innerHTML = '';
        devices.forEach(device => {
            const row = document.createElement('tr');
            row.innerHTML = `
                    <td>${device.deviceNumber}</td>
                    <td>${device.deviceName}</td>
                    <td>${device.deviceType}</td>
                    <td>${device.manufacturer}</td>
                    <td>${device.manufactureDate || ''}</td>
                    <td>${device.warrantyPeriod}</td>
                    <td>${device.afterSalesContact}</td>
                    <td>${device.afterSalesPhone}</td>
                    <td>
                        <div class="action-buttons">
                            <button class="btn btn-sm btn-secondary" onclick="viewDeviceDetail(${device.id})">详情</button>
                        </div>
                    </td>
                `;
            tbody.appendChild(row);
        });
    }
    function viewDeviceDetail(id) {
        getDeviceById(id).then(device => {
            if (device) {
                document.getElementById('detailDeviceNumber').textContent = device.deviceNumber || '-';
                document.getElementById('detailDeviceName').textContent = device.deviceName || '-';
                document.getElementById('detailDeviceType').textContent = device.deviceType || '-';
                document.getElementById('detailManufacturer').textContent = device.manufacturer || '-';
                document.getElementById('detailManufactureDate').textContent = device.manufactureDate || '-';
                document.getElementById('detailWarrantyPeriod').textContent = device.warrantyPeriod || '-';
                document.getElementById('detailAfterSalesContact').textContent = device.afterSalesContact || '-';
                document.getElementById('detailAfterSalesPhone').textContent = device.afterSalesPhone || '-';
                document.getElementById('detailWarrantyDesc').textContent = device.warrantyDesc || '-';
                showDeviceDetailModal();
            }
        });
    }
    async function getDeviceById(id) {
        try {
            const response = await fetch(`http://localhost:8080/api/devices/${id}`);
            const device = await response.json();
            return device;
        } catch (error) {
            console.error('获取设备详情失败:', error);
            return null;
        }
    }

    function logout() {
        localStorage.removeItem('userInfo');
        window.location.href = '../../../login.html';
    }
    // 显示iOS风格模态框
    function showDeviceDetailModal() {
        const modal = document.getElementById('deviceDetailModal');
        modal.classList.add('show');
        modal.onclick = function (e) {
            if (e.target === modal) {
                closeDeviceDetailModal();
            }
        };
    }
    // 关闭iOS风格模态框
    function closeDeviceDetailModal() {
        const modal = document.getElementById('deviceDetailModal');
        modal.classList.remove('show');
    }
    // ESC键关闭模态框
    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape') {
            closeDeviceDetailModal();
        }
    });
</script>
</body>
</html>